//@formatter:off
/*********************************************************************************************************************************************************************************
 * #######################
 * #   FILE DESCRIPTOR   #
 * #######################
 * Application: BiblioteQEJB
 * Package: org.biblioteq.ejb.entities
 * File: User.java
 * 
 * #######################
 * #   GNU DISCLAIMER    #
 * ####################### 
 * BiblioteQ Online is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version. BiblioteQ Online is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
 * 
 * #######################
 * #      Revision       #
 * ####################### 
 * Mar 3, 2012, Clinton Bush, 1.0.0,
 *    New file.
 * 
 ********************************************************************************************************************************************************************************** 
 */
//@formatter:on
package org.biblioteq.ejb.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.log4j.Logger;

/**
 * Entity representing entries of the "online_user" table.
 * 
 * @author Clint Bush
 * 
 */
@Entity(name = "User")
@Table(name = "online_user")
public class User implements Serializable
{
	/**
	 * Get the logger.
	 */
	private static Logger log = Logger.getLogger(User.class);
	
	/**
	 * GUID for implementing Serializable
	 */
	private static final long serialVersionUID = 8803364975961007856L;
	
	private int id = -1;
	private String userName = "";
	private String password = "";
	private Member member;
	private Admin admin;
	private boolean active = false;
	private Date dateCreated;
	private Date dateUpdated;
	private User createdBy;
	private User updatedBy;
	
	/**
	 * If the user is an administrator of some sort, this returns the Admin object representing that information; otherwise, null.
	 * 
	 * @return (Admin) The Admin object with an administrator user's information; null if the user is not an administrator.
	 */
	@OneToOne
	@JoinColumn(name = "adminusername", unique = true, nullable = true, referencedColumnName = "username")
	public Admin getAdmin()
	{
		return this.admin;
	}
	
	/**
	 * The online user who created this account. Null if it was created automatically by the user to whom it belongs.
	 * 
	 * @return (User) The user who created the account; null if it was auto-created.
	 */
	@ManyToOne
	public User getCreatedBy()
	{
		return this.createdBy;
	}
	
	/**
	 * The date this account was originally created.
	 * 
	 * @return (Date) Date account was created.
	 */
	@Column(name = "datecreated")
	public Date getDateCreated()
	{
		return this.dateCreated;
	}
	
	/**
	 * The date the account was last updated.
	 * 
	 * @return (Date) Date of last account update.
	 */
	@Column(name = "dateupdated")
	public Date getDateUpdated()
	{
		return this.dateUpdated;
	}
	
	@Id
	@SequenceGenerator(initialValue = 1, name = "online_user_id_seq", sequenceName = "online_user_id_seq", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "online_user_id_seq")
	@Column(name = "id")
	public int getId()
	{
		return this.id;
	}
	
	/**
	 * If the user is a library member, get the Member object generated by BiblioteQ Desktop for the library member.
	 * 
	 * @return (Member) The library member represented by this User.
	 */
	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "memberid", unique = true, nullable = true, referencedColumnName = "memberid")
	public Member getMember()
	{
		return this.member;
	}
	
	/**
	 * Get the password. This is an MD5 password that must be de-hashed using some of the security methods in BiblioteQ Online.
	 * 
	 * @return (String) MD5 password.
	 */
	@Column(name = "password")
	public String getPassword()
	{
		return this.password;
	}
	
	/**
	 * The online user who last updated this account.
	 * 
	 * @return (User) The user who last updated this account.
	 */
	@ManyToOne
	public User getUpdatedBy()
	{
		return this.updatedBy;
	}
	
	/**
	 * Get the user name.
	 * 
	 * @return (String) User name.
	 */
	@Column(name = "username")
	public String getUserName()
	{
		return this.userName;
	}
	
	/**
	 * True if the account is active and can be logged into; otherwise, false.
	 * 
	 * @return (boolean) True for active account; false for inactive.
	 */
	@Column(name = "active")
	public boolean isActive()
	{
		return this.active;
	}
	
	/**
	 * Sets the account's active/inactive status. True for active; false for inactive.
	 * 
	 * @param active
	 *            (boolean) Active/Inactive status.
	 */
	public void setActive(boolean active)
	{
		this.active = active;
	}
	
	/**
	 * Sets an the Admin object for this user to indicate that the user has an administrator account in BiblioteQ Desktop.
	 * 
	 * @param adminUserName
	 *            (Admin) An object with information about this user's administrator privileges (if the user is an administrator).
	 */
	public void setAdmin(Admin admin)
	{
		this.admin = admin;
	}
	
	/**
	 * Sets the User who created this account.
	 * 
	 * @param createdBy
	 *            (User) The user who created this account.
	 */
	public void setCreatedBy(User createdBy)
	{
		this.createdBy = createdBy;
	}
	
	/**
	 * Sets the date the account was created.
	 * 
	 * @param dateCreated
	 *            (Date) The date the account was created.
	 */
	public void setDateCreated(Date dateCreated)
	{
		this.dateCreated = dateCreated;
	}
	
	/**
	 * Sets the date the account was last updated.
	 * 
	 * @param dateUpdated
	 *            (Date) The date the account was last updated.
	 */
	public void setDateUpdated(Date dateUpdated)
	{
		this.dateUpdated = dateUpdated;
	}
	
	/**
	 * Sets the ID of this User. Should be private and never used.
	 * 
	 * @param id
	 *            (int) ID of the User.
	 */
	private void setId(int id)
	{
		this.id = id;
	}
	
	/**
	 * Sets the member to whom this online account belongs. If this is an administrator's account, this should be null.
	 * 
	 * @param member
	 *            (Member) The member to whom this online account belongs.
	 */
	public void setMember(Member member)
	{
		this.member = member;
	}
	
	/**
	 * Sets the user's password. This should only be set to a value generated by the security utilities of this application. This should be an MD5 hashed password.
	 * 
	 * @param password
	 *            (String) The user's MD5 hashed password.
	 */
	public void setPassword(String password)
	{
		this.password = password;
	}
	
	/**
	 * Sets the user who last updated this account.
	 * 
	 * @param updatedBy
	 *            (User) The user who last updated this account.
	 */
	public void setUpdatedBy(User updatedBy)
	{
		this.updatedBy = updatedBy;
	}
	
	/**
	 * Sets the username used by the user to log in.
	 * 
	 * @param userName
	 *            (String) The username.
	 */
	public void setUserName(String userName)
	{
		this.userName = userName;
	}
	
}
